package com.easytopit.demo.spring.mvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.easytopit.demo.spring.mvc.service.UserService;

@Controller
//@RequestMapping("/user")
public class HelloWorldController {

	@Autowired
	private UserService userService;
	
//	@GetMapping("/index")
//	public String index(){
//		return "hello";
//	}
	
	@GetMapping("/index")
	public ModelAndView index(ModelAndView modelAndView){
		modelAndView.addObject("m", "传递的参数");
		modelAndView.setViewName("hello");
		return modelAndView;
	}

//	@RequestMapping(value="/add.htm") 
//	@RequestMapping(value="/hello", method=RequestMethod.POST) 
	@RequestMapping(value="/hello", method=RequestMethod.GET) // 只允许GET方法使用
//	@GetMapping("/hello")
//	@PostMapping("/hello")
	public String showMessage(String name/* 请求参数name */, ModelMap model/* 模型数据 */) {
		// 传输到JSP页面:WEB-INFO/views/hello.jsp
		model.put("name", name);
		return "hello"; // WEB-INFO/views/hello.jsp
	}

	@RequestMapping("/names")
	public String showNames(ModelMap model) {
		model.put("names", userService.getUsers());
		return "name";
	}
	
	@RequestMapping("/form.htm")
	public String htm() {
		return "form";
	}

	
}